﻿using CJAtech.BBS.Business;
using CJAtech.BBS.DataEntity;
using CJAtech.BBS.Process;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bigger.Framework;
using Bigger.Base.Cache;
using Bigger.Web.Helper;

namespace CJAtech.BBS.Web.Controllers
{
    [BBSConfigFilter("geely")]
    [SSOAuthFilter(BBSPowerType.CheckLogin)]
    public class GeeLyController : BBSBaseController
    {
        public override BBSBaseProcess CreateBBSProcess()
        {
            return new BBSBaseProcess("geely");
        }


        [SSOAuthFilter(BBSPowerType.None)]
        public ActionResult LoginOut()
        {
            SSOProcess objSSOProcess = new SSOProcess(CurrentBBSProcess.CurrentCommunityConfig);
            string token = CookieHelper.GetCookieValue(CurrentBBSProcess.CurrentCommunityConfig.ConfigCode + "SSOToken");
            InvokeResult objInvokeResult = new InvokeResult();
            if (token.IsNull())
            {
                objInvokeResult.ResultCode = "TokenNull";
                objInvokeResult.ResultMessage = "注销失败，Token为空";
                return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
            }
            objSSOProcess.LoginOut(token);
            objInvokeResult.ResultMessage = "注销成功";
            return Json(objInvokeResult, JsonRequestBehavior.AllowGet);
        }

        [SSOAuthFilter(BBSPowerType.None)]
        public ActionResult Target(string token, string sign, string targetUrl)
        {
            if (targetUrl.IsNull())
            {
                return Http404();
            }
            targetUrl = targetUrl.DecodeUrl();

            SSOProcess objSSOProcess = new SSOProcess(CurrentBBSProcess.CurrentCommunityConfig);

            if (!objSSOProcess.ValidTicket())
            {
                return Content("对不起，票据不合法");
            }

            UserInfo objUserInfo = objSSOProcess.GetUserInfoConvert(token);
            if (objUserInfo == null)
            {
                return Redirect(objSSOProcess.CreateRedirectConvertUrl(targetUrl));
            }

            CookieHelper.SetCookieValue(token, CurrentBBSProcess.CurrentCommunityConfig.ConfigCode + "SSOToken");

            return Redirect(targetUrl);
        }

        public virtual ActionResult GetCurrentUserInfo()
        {
            UserInfo objUserInfo = CurrentBBSProcess.GetUserInfo();
            return Json(objUserInfo, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 近期活动
        /// </summary>
        /// <returns></returns>
        public ActionResult Recent()
        {
            return View();
        }

        /// <summary>
        /// 活动栏目
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult PublicTraining(int? id)
        {
            if (!id.HasValue)
            {
                return Http404();
            }
            CommunityConfig objCommunityConfig = CurrentBBSProcess.CurrentCommunityConfig;
            BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo(objCommunityConfig.ConnectionStringKey, objCommunityConfig.DataObjectParam);
            CategoryInfo objCategoryInfo = objBizCategoryInfo.GetRecordCache("", id.Value, TimeSpan.FromSeconds(10));
            if (objCategoryInfo == null)
            {
                return Http404();
            }
            ViewBag.CategoryInfo = objCategoryInfo;
            List<CategoryInfo> objList = objBizCategoryInfo.GetListCache("", TimeSpan.FromSeconds(10), "ParentId=" + id.Value + " AND IsRelease=1");
            return View(objList);
        }

        /// <summary>
        /// 活动列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult RecentActivities(int? id)
        {
            if (!id.HasValue)
            {
                return Http404();
            }
            CommunityConfig objCommunityConfig = CurrentBBSProcess.CurrentCommunityConfig;
            BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo(objCommunityConfig.ConnectionStringKey, objCommunityConfig.DataObjectParam);
            CategoryInfo objCategoryInfo = objBizCategoryInfo.GetRecordCache("", id.Value, TimeSpan.FromSeconds(10));
            if (objCategoryInfo == null)
            {
                return Http404();
            }
            return View(objCategoryInfo);
        }

        /// <summary>
        /// 活动详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ActivityDetail(int? id)
        {
            if (!id.HasValue)
            {
                return Http404();
            }
            CommunityConfig objCommunityConfig = CurrentBBSProcess.CurrentCommunityConfig;
            ArticleInfoProcess objArticleInfoProcess = new ArticleInfoProcess(objCommunityConfig.ConnectionStringKey, objCommunityConfig.DataObjectParam);
            ArticleResultInfo objArticleResultInfo = objArticleInfoProcess.GetArticleResultInfo(id.Value, true, false);
            if (objArticleResultInfo == null || objArticleResultInfo.ArticleInfo == null)
            {
                return Http404();
            }
            UserInfo objUserInfo = this.CurrentBBSProcess.GetUserInfo();
            if (objArticleResultInfo.ArticleInfo.IsRelease != 1 && !string.Equals(objArticleResultInfo.ArticleInfo.ObjectId, objUserInfo.UserId))
            {
                return Content("对不起，当前文章还在审核中");
            }
            //更新文章的浏览数
            objArticleInfoProcess.IncreaseViewCount(id.Value);
            return View(objArticleResultInfo);
        }
    }
}
